package com.novel.common.job;

import com.novel.common.pojo.RedisParamKey;
import com.novel.mapper.NovelMapper;
import com.novel.pojo.novel.Novel;
import com.novel.service.NovelService;
import com.novel.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 同步小说点击量
 */
@Slf4j
@Component
public class AddNovelClickJob extends QuartzJobBean {

    @Autowired
    private NovelMapper novelMapper;

    @Autowired
    private NovelService novelService;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
        //查询所有小说信息
        List<Novel> novelList = novelMapper.selectAll();
        //遍历操作
        novelList.forEach(novel -> {
            //从redis中获取小说点击量
            Long click = Long.parseLong(redisUtils.hashGet(novel.getNovelId(), RedisParamKey.novelClick));
            //对比小说点击量相同跳过，不同继续
            if (novel.getNovelClick().equals(click)) {
                return;
            }
            novelService.updateNovelClickCount(novel.getNovelId(), click);

        });

    }
}